function [new_mu new_sigma] = conditionGaussian(mu, sigma, x, conditioned)

if conditioned == 'head'
    % first part conditioned on known values for the second part
    unknown = 1:(length(mu) - size(x, 2));
    doknown = unknown(end)+(1:size(x, 2));
elseif conditioned == 'tail'
    % second part conditioned on known values for the first part
    unknown = (size(x, 2)+1):length(mu);
    doknown = 1:(unknown(1)-1);
else
    error('invalid conditioning', 'invalid conditioning');
end

new_mu = repmat(mu(unknown), size(x, 1), 1)' + sigma(unknown, doknown)*inv(sigma(doknown, doknown))*(x-repmat(mu(doknown), size(x, 1), 1))';
new_sigma = sigma(unknown, unknown) - sigma(unknown, doknown)*inv(sigma(doknown, doknown))*sigma(doknown, unknown);

new_mu = new_mu';
